<template>
  <div class="publish-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ name: 'home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{
            $route.query.id ? "修改文章" : "发布文章 "
          }}</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 面包屑 -->
      </div>

      <!-- 数据表单 -->
      <el-form
        :model="article"
        :rules="formRules"
        ref="publish-from"
        label-width="60px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="article.title"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            placeholder="请输入文章内容"
            type="textarea"
            v-model="article.content"
          ></el-input>
        </el-form-item>
        <el-form-item label="封面">
          <el-radio-group v-model="article.cover.type">
            <el-radio :label="1">单图</el-radio>
            <el-radio :label="3">三图</el-radio>
            <el-radio :label="0">无图</el-radio>
            <el-radio :label="-1">自动</el-radio>
          </el-radio-group>
          <!-- 
            我们需要把选择的封面图片的地址放到article.cover.images数组中

            当你给事件处理函数传递了自定义参数以后，就无法得到原本的那个数据参数了

            如果想要在事件处理函数自定义传参以后还想得到原来的那个事件本身的参数，则手动指定$event,它就代表那个事件本身的参数,在自定义事件中表现为捕获从子组件抛出的值


           -->
          <template v-if="article.cover.type > 0">
            <upload-cover
            :key="cover"
            v-for="(cover, index) in article.cover.type"
            :cover-image="article.cover.images[index]"
            @update-cover="onUpdateCover(index, $event)"
            />
          </template>
        </el-form-item>

        <el-form-item label="频道" prop="channel_id">
          <el-select v-model="article.channel_id" placeholder="请选择">
            <el-option
              :label="channel.name"
              :value="channel.id"
              v-for="(channel, index) in channels"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onAdd(false)">发表 </el-button>
          <el-button @click="onAdd(true)">存入草稿 </el-button>
        </el-form-item>
      </el-form>
      <!-- 数据表单 -->
    </el-card>
  </div>
</template>

<script>
import UploadCover from "./components/upload-cover.vue";
import {
  getArticlesChannels,
  addArticle,
  getArticle,
  updateArticle,
} from "@/api/article.js";

export default {
  name: "PublishIndex",
  components: {
    UploadCover,
  },
  data() {
    return {
      article: {
        title: "", //文章标题
        content: "", //文章内容
        cover: {
          //文章封面 对象类型
          type: 0, // 封面类型 -1：自动，0-无图，1-1张，3-3张
          images: [], // 封面图片的地址，空数组
        },
        channel_id: null,
      },
      channels: [], //文章频道列表
      formRules: {
        // blur失去焦点
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 5,
            max: 30,
            message: "长度在 5 到 30 个字符",
            trigger: "blur",
          },
        ],
        content: [
          { required: true, message: "请输入文章内容", trigger: "blur" },
        ],
        channel_id: [{ required: true, message: "请选择频道" }],
      },
    };
  },
  created() {
    this.loadChannels();

    // 由于我们让发布和修改使用的同一个组件
    // 所以这里要判断
    // 如果路由路径参数中有 id 则请求展示文章内容
    if (this.$route.query.id) {
      this.loadArticle();
    }
  },
  methods: {
    loadChannels() {
      getArticlesChannels().then((res) => {
        // console.log(res);
        this.channels = res.data.data.channels;
      });
    },
    // 手动表单验证
    onAdd(draft) {
      this.$refs["publish-from"].validate((valid) => {
        // 如果验证失败，停止请求提交
        if (!valid) {
          return;
        } else {
          this.onPublish(draft);
        }
      });
    },

    onPublish(draft) {
      // 找到数据接口
      // 封装请求方法
      // 请求提交表单

      // 如果是修改文章 则执行修改操作，否则执行添加操作
      const articleId = this.$route.query.id;
      if (articleId) {
        // 执行修改操作
        updateArticle(articleId, this.article, draft).then((res) => {
          console.log(res);
          this.$message({
            message: `${draft ? "存入草稿" : "发布"}成功`,
            type: "success",
          });
          this.$router.push("/article");
        });
      } else {
        addArticle(this.article, draft).then((res) => {
          // console.log(res);
          this.$message({
            message: `${draft ? "存入草稿" : "发布"}成功`,
            type: "success",
          });
          this.$router.push("/article");
        });
      }

      // 处理响应结果
    },
    // 修改文章：加载文章内容
    loadArticle() {
      console.log("loadArticle");
      // 找到数据接口
      // 封装请求方法
      // 请求获取数据
      getArticle(this.$route.query.id).then((res) => {
        // 模板绑定展示
        console.log(res);
        this.article = res.data.data;
      });
    },

    onUpdateCover (index, url) {
      
      this.article.cover.images[index] = url
    }
  },
};
</script>

<style>
</style>